package com.session02.pm.books;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class BookManager {
    Book[] books = new Book[50];
    Scanner input = new Scanner(System.in);

    public BookManager() {
        books[0] = new Book(1, "斗破苍穹", 1, 100, null);
        books[1] = new Book(2, "斗罗大陆", 0, 90, "2022-01-11");
        books[2] = new Book(3, "甜心大少", 0, 99, "2022-02-23");
        books[3] = new Book(4, "霸道总裁", 1, 80, null);
        books[4] = new Book(5, "都市公子", 0, 81, "2022-04-01");
    }

    public void menu() {
        String answer = "y";
        while (answer.equalsIgnoreCase("y")) {
            System.out.println("欢迎使用图书管理系统");
            System.out.println("--------------------");
            System.out.println("1.查看图书");
            System.out.println("2.新增图书");
            System.out.println("3.删除图书");
            System.out.println("4.借出图书");
            System.out.println("5.归还图书");
            System.out.println("6.排行榜");
            System.out.println("0.返回主菜单");
            System.out.println("--------------------");
            System.out.println("请选择0-6:");
            int choose = input.nextInt();
            switch (choose) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    lend();
                    break;
                case 5:
                    returnBook();
                    break;
                case 6:
                    sort();
                    break;
                case 0:
                    System.exit(0);
            }
            System.out.println("请问你是否继续游戏(y/n)");
            answer = input.next();
        }
    }

    public void sort() {
        Book[] temp = new Book[50];
        int num = 0;
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                num++;
            }
        }
        for (int i = 0; i < num - 1; i++) {
            temp[i] = new Book();
            for (int j = 0; j < num - 1 - i; j++) {
                if (books[j].count < books[j + 1].count) {
                    temp[i] = books[j];
                    books[j] = books[j + 1];
                    books[j + 1] = temp[i];
                }
            }
        }
    }

    public void returnBook() {
        System.out.println("请输入你要归还图书的名字：");
        String returnName = input.next();
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                if (books[i].name.equals(returnName) && books[i].status == 0) {
                    System.out.println("请输入归还的时间:");
                    String returnTime = input.next();

                    long loan = getLoan(books[i].time, returnTime);
                    System.out.println("归还成功:");
                    System.out.println("租金：" + loan);
                    books[i].status = 1;
                    books[i].time = null;
                    break;
                } else if (books[i].name.equals(returnName) && books[i].status == 1) {
                    System.out.println("该图书没有被借出，你是不是傻~");
                    break;
                }
            }
        }
    }

    public long getLoan(String time, String returnTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long diff = 0;
        try {
            Date d1 = sdf.parse(time);
            Date d2 = sdf.parse(returnTime);
            diff = (d2.getTime() / (1000 * 60 * 60 * 24)) - (d1.getTime() / (1000 * 60 * 60 * 24));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return diff;
    }

    public void lend() {
        int index = -1;
        System.out.println("请输入你要借出图书的名字");
        String lendName = input.next();
        int flag = 0;
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                if (books[i].name.equals(lendName) && books[i].status == 1) {
                    index = i;
                    flag = 1;
                    break;
                } else if (books[i].name.equals(lendName) && books[i].status == 0) {
                    flag = -1;
                }
            }
        }
        if (flag == 1) {
            books[index].status = 0;
            books[index].count++;
            System.out.println("请输入你要借出的时间:");
            books[index].time = input.next();

            System.out.println("借出成功！");
        } else if (flag == -1) {
            System.out.println("找到你要借出的图书，但是该图书已经被借出，不能重复借出");
        } else {
            System.out.println("没有找到您要借出的图书");
        }
    }

    public void delete() {
        System.out.println("请输入你要删除书的名字");
        String delName = input.next();
        boolean flag = false;
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                if (books[i].name.equals(delName) && books[i].status == 1) {
                    int j = i;
                    while (books[j + 1] != null) {
                        books[j].id = books[j + 1].id;
                        books[j].name = books[j + 1].name;
                        books[j].status = books[j + 1].status;
                        books[j].count = books[j + 1].count;
                        books[j].time = books[j + 1].time;
                        j++;
                    }
                    //最后一本书
                    books[j] = null;
                    flag = true;
                }
            }
        }
        if (flag) {
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败，没有你要删除的图书或者该图书是借出状态");
        }

    }

    public void add() {
        int index = -1;
        for (int i = 0; i < books.length; i++) {
            if (books[i] == null) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            books[index] = new Book();
            System.out.println("请输入新图书的id");
            books[index].id = input.nextInt();
            System.out.println("请输入新图书的名字");
            books[index].name = input.next();
            books[index].status = 1;
            books[index].count = 1;
            books[index].time = null;
        } else {
            System.out.println("图书收藏已经满了...");
        }
    }

    public void list() {
        String str = "";
        String times = "";
        for (int i = 0; i < books.length; i++) {
            boolean flag = false;
            if (books[i] != null) {
                if (books[i].status == 0) {
                    str = "已经被借出";
                    times = books[i].time;
                    flag = true;
                } else {
                    str = "未被借出";
                }
                if (flag) {
                    System.out.println("图书的id：" + books[i].id + " 图书的名字：" + books[i].name +
                            " 图书的状态：" + str + " 图书的数量：" + books[i].count + " 图书借出时间：" + times);
                } else {
                    System.out.println("图书的id：" + books[i].id + " 图书的名字：" + books[i].name +
                            " 图书的状态：" + str + " 图书的数量：" + books[i].count);
                }
            }
        }
    }
}
